import tornado
import pymysql
from tornado import gen
from tornado.options import options

class MySQLConnection:
    mysql_host = None
    mysql_port = None
    mysql_user = None
    mysql_pwd = None
    mysql_db = None

    @staticmethod
    def load_config():
        MySQLConnection.mysql_host = tornado.options.options.mysql_host
        MySQLConnection.mysql_port = tornado.options.options.mysql_port
        MySQLConnection.mysql_user = tornado.options.options.mysql_user
        MySQLConnection.mysql_pwd = tornado.options.options.mysql_pwd
        MySQLConnection.mysql_db = tornado.options.options.mysql_db

    @staticmethod
    def get_connection():
        if MySQLConnection.mysql_host is None:
            MySQLConnection.load_config()
            
        conn = pymysql.connect(host=MySQLConnection.mysql_host,
                               port=MySQLConnection.mysql_port,
                               user=MySQLConnection.mysql_user,
                               password=MySQLConnection.mysql_pwd,
                               db=MySQLConnection.mysql_db,
                               charset='utf8',
                               cursorclass=pymysql.cursors.DictCursor)
        return conn
        